package com.jtpay.unionpay.product.dbUtils;

import com.jtpay.unionpay.product.ProductBeanDao;
import com.jtpay.unionpay.product.model.ProductBean;
import org.greenrobot.greendao.query.Query;

/**
 * Description:
 * File: UnionPayProductDbUtil.java
 * Author: Administrator
 * Create: 2018/07/30 15:08
 */
public class UnionPayProductDbUtil {

    private Query<ProductBean> query;

    public synchronized ProductBean getOneForProduct() {
        ProductBean bean = getOneForProducing();
        if (bean != null) {
            return bean;
        }
        query = CustomDbUtils.getInstance().query(
                ProductBeanDao.Properties.Money.isNotNull(),
                ProductBeanDao.Properties.Treated.eq("0"));
        if (query.list().size() > 0) {
            bean = query.list().get(0);
            bean.setTreated("1");
            CustomDbUtils.getInstance().update(bean);
            return bean;
        }
        return null;
    }

    private synchronized ProductBean getOneForProducing() {
        query = CustomDbUtils.getInstance().query(
                ProductBeanDao.Properties.Money.isNotNull(),
                ProductBeanDao.Properties.Treated.eq("1"));
        if (query.list().size() > 0) {
            return query.list().get(0);
        }
        return null;
    }

    public synchronized void update(ProductBean payQrModel) {
        if (payQrModel == null) {
            return;
        }
        payQrModel.setTreated("2");
        CustomDbUtils.getInstance().update(payQrModel);
    }
}
